package com.gxa.eshop.portal.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;

@Configuration
public class ActiveMQConfig
{

    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;

    @Value("${spring.activemq.user}")
    private String username;

    @Value("${spring.activemq.password}")
    private String password;


    @Bean
    public ConnectionFactory connectionFactory(){
        return new ActiveMQConnectionFactory(username, password, brokerUrl);
    }

    //当监听器创建时,加载容器工厂,目的是为了设定事务和签收模式
    @Bean(name = "simpleFactory")
    public SimpleJmsListenerContainerFactory firstFactory(ConnectionFactory  connectionFactory) {
        SimpleJmsListenerContainerFactory factory=new SimpleJmsListenerContainerFactory();
        //关闭事务
        factory.setSessionTransacted(false);
        // 开启手动签收
        factory.setSessionAcknowledgeMode(4);
        factory.setConnectionFactory(connectionFactory); //通过 参数注入
        return factory;
    }


}